/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.pipelines;

import biofilter.filters.RecordsFilter;
import biofilter.records.Record;
import biofilter.sources.RecordsSourceIF;
import biofilter.filters.CompositeFilter;
import java.util.Vector;
import java.util.LinkedList;
import org.apache.commons.cli.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * PipelineBase contains core functionality to be used across pipelines.
 * examples include:
 * -parameters
 * -status (i.e. # filters, execution time)
 * -loging
 * @author Daniel Quest
 * @date   Dec 31 2009
 */
public class PipelineBase implements PipelineIF {

    public PipelineBase(){
        filters = new CompositeFilter();
    }
    public PipelineBase(String[] args){
        try {
            filters = new CompositeFilter();
            setup(args);
        }catch(org.apache.commons.cli.ParseException pe){
            System.err.print(pe);
        }
    }

    protected Logger logger             = null;


    /**
     * The CompositeFilter contains the source and filters for the pipeline.
     * to set the pipeline source, set the source for the composite filter
     * to add filters to the pipeline, add filters to the composite filter.
     */
    protected CompositeFilter filters = null;


    /**
     * Derived class must overide execPipeline.
     * @return
     * @throws Exception
     */
    public Vector<Record> execPipeline() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }


//    public boolean eof() throws Exception {
//        if ( filters == null ) { throw new java.lang.Exception("PipelineIF not set up correctly prior to call to eof()"); }
//        return filters.getLast().eof();
//    }

    /**
     * default behavior is to deligate getRecord() to the last filter in the
     * pipeline stack.
     * @return
     */
     public Record getRecord() throws Exception {
        if ( filters == null ) { throw new java.lang.Exception("Pipeline not set up correctly prior to call to getRecord()"); }
        return filters.getRecord();
    }

    /**
     * This is a generic setup function.  Specific pipelines may need to implement similar logic for specific options they wish to implement
     */
    protected Options options = null;
    public void setup(String [] args) throws org.apache.commons.cli.ParseException{
            options = new Options();

            //Setup Options
            //Boolean Options
            Option help = new Option( "h", "help", false, "print help message" );
      
            //Param Options
            Option infile   = new Option("i", "infile", true, "concrete source file e.g. a line delimited file containing results" );
            Option outfile   = new Option("o", "outfile", true, "concrete sink file e.g. a line delimited file containing results" );

            //Add command line options to influence behavior
            options.addOption( help );
            options.addOption(infile);
            options.addOption(outfile);

            CommandLineParser parser = new PosixParser();
            CommandLine cmd = parser.parse( options, args);

            //Logic to handle standard command line options such as help

            if (cmd.hasOption("help") == true || cmd.hasOption("infile") == false || cmd.hasOption("outfile") == false){
                // automatically generate the help statement
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp( "SeqGenOpts", options );
                System.exit(0);
            }

    }

/*
    public void main(String[] args) {
        try {
            PipelineBase p = new PipelineBase(args);
            p.execPipeline();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
*/

}
